import TipsPage from "../Page/TipsPage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class tipsPageSpNode extends cc.Component {

    @property({type:cc.Node,displayName:"mask节点"})
    maskNode:cc.Node = null;

    protected onLoad(): void {
        this.node.on('touchstart',this.onTouchStart,this);
        this.node.on('touchmove',this.onTouchMove,this);
        this.node.on('touchend',this.onTouchEnd,this);
        this.node.on('touchcancel',this.onTouchCancel,this);
    }

    sensitivityNum: number = 0.5;

    oldScale: number = 0.3;

    touchOne:cc.Touch = null;
    touchTwo:cc.Touch = null;

    @property({type:TipsPage,displayName:"提示界面"})
    tipsPage:TipsPage = null;

    @property({displayName:"最大缩放倍数"})
    maxScale:number = 2;

    @property({type:cc.Float})
    moveMul:number =1;

    @property({type:cc.Float})
    scaleMul:number = 1;

    onTouchStart(e:cc.Event.EventTouch){
        if(!this.touchTwo&&this.touchOne) this.touchTwo = e.touch;
        if(!this.touchOne) this.touchOne = e.touch;

        this.isTouchTwo = false;
    }

    isTouchTwo:boolean = false;

    onTouchMove(e:cc.Event.EventTouch){
        e.stopPropagation();
        // if (Normallevel134._instance.isPlaySound) return;
        let touches = e.getTouches();
        let touchPos: cc.Vec2 = e.getLocation();
        let startTouchPos = e.getStartLocation();
        if(touchPos.sub(startTouchPos).mag()>20){
            this.isTouchTwo = true;
        }
        if (touches.length == 1) {
            let maxXDistance:number = ((this.node.width*this.node.scale) - this.node.width)/2;
            let maxYDistance:number = ((this.node.height*this.node.scale) - this.node.height)/2;
            let delta = e.getDelta().mulSelf(this.moveMul);

            let dx = delta.x;
            let dy = delta.y;

            if(this.node.x + dx < -maxXDistance){
                dx = 0;
            }
            if(this.node.x + dx >maxXDistance){
                dx = 0;
            }

            if(this.node.y + dy < -maxYDistance){
                dy = 0;
            }
            if(this.node.y + dy >maxYDistance){
                dy = 0;
            }

            this.node.x += dx;
            this.node.y += dy;
        }

        if (touches.length == 2) {
            let touch1 = touches[0], touch2 = touches[1];
            let delta1 = touch1.getDelta();
            let delta2 = touch2.getDelta();
            let touchPoint1 = this.node.convertToNodeSpaceAR(touch1.getLocation())
            let touchPoint2 = this.node.convertToNodeSpaceAR(touch2.getLocation())
            let distance = touchPoint1.sub(touchPoint2)
            let delta = delta1.sub(delta2).mulSelf(this.scaleMul);
            let scale = 1;
            if (Math.abs(distance.x) > Math.abs(distance.y)) {
                scale = (distance.x + delta.x) / distance.x * this.node.scaleX;
            }
            else {
                scale = (distance.y + delta.y) / distance.y * this.node.scaleY;
            }

            let pos = touchPoint2.add(cc.v2(distance.x / 2, distance.y / 2))
            //console.log("pos:",Math.floor(pos.x),Math.floor(pos.y));
            //滑轮缩放大小
            let scX = scale;
            //当前缩放值与原来缩放值之差
            let disScale = scX - this.node.scaleX;
            //当前点击的坐标与缩放值差像乘 
            let gapPos = pos.scale(cc.v2(disScale, disScale));
            //当前node坐标位置减去点击 点击坐标和缩放值的值
            let mapPos = this.node.getPosition().sub(gapPos);
            //console.log("mapPos:",Math.floor(mapPos.x),Math.floor(mapPos.y));

            //放大缩小
            if (this.isMinScale(scale)) return;
            if (this.isMaxScale(scale)) return;
            this.node.setScale(scale, scale)

            // let pre3 = cc.instantiate(this.redPre)
            // this.pre3 = pre3;
            // pre3.setPosition(pos)
            // pre3.scaleX = 2
            // pre3.scaleY = 2
            // this.node.addChild(pre3);
            //移动地图坐标
            this.node.setPosition(mapPos)
            this.fixPos(cc.v2(0, 0))
        }
    }

    isAni:boolean = false;

    clickCount:number = 0;

    onTouchEnd(e:cc.Event.EventTouch){
        // let touchs = e.getTouches();
        if(this.isTouchTwo) return;
        // console.log(touchs.length,"?????????????????")
        // console.log("松开节点????????????");
        // if (this.node.scale <= 1) {
        //     this.node.scale = 1;
        //     this.node.setPosition(cc.Vec3.ZERO);
        // }
        if(this.isAni) return;
        this.clickCount ++;
        if(this.clickCount==1){
            this.scheduleOnce(this.hideSp,0.2);
            // this.clickCount = 0;
        }else{
            this.unschedule(this.hideSp);
            this.isAni = true;
            this.clickCount =0;
            if(this.node.scale>1){
                cc.tween(this.node).to(0.2,{scale:1,x:0,y:0}).call(()=>{
                    this.isAni = false;
                }).start();
            }else{
                cc.tween(this.node).to(0.2,{scale:1.5}).call(()=>{
                    this.isAni = false;
                }).start();
            }
        }
    }    

    protected onDisable(): void {
        this.node.scale = 1;
        this.isAni = false;
        this.isTouchTwo = false;
    }

    hideSp(){
        this.clickCount = 0;
        this.tipsPage?.closeSpScale();
        this.tipsPage?.closeAnswerScale();
    }

    onTouchCancel(e:cc.Event.EventTouch){
        // console.log("离开节点?????????");
        // if (this.node.scale <= 1) {
        //     this.node.scale = 1;
        //     this.node.setPosition(cc.Vec3.ZERO);
        // }
    }

    //修正坐标
    fixPos(delta) {
        let x = this.node.x + delta.x;
        let y = this.node.y + delta.y;
        let cW = this.node.width * this.node.scaleX
        let cH = this.node.height * this.node.scaleY

        let winSize = cc.size(this.node.getContentSize())//cc.winSize

        let xMin = -cW / 2 + winSize.width / 2;
        let xMax = cW / 2 - winSize.width / 2;
        let yMin = -cH / 2 + winSize.height / 2;
        let yMax = cH / 2 - winSize.height / 2;

        x = x > xMax ? xMax : x
        x = x < xMin ? xMin : x;
        y = y > yMax ? yMax : y;
        y = y < yMin ? yMin : y;

        //console.log("移动",x,y);
        // cc.vv.mapPos = cc.v2(x, y);
        // cc.vv.mapScale = this.node.scaleX;
        this.node.setPosition(cc.v2(x, y))
    }

    //是否最大缩放
    isMaxScale(sc) {
        return sc > this.maxScale;
    }
    //是否最小缩放
    isMinScale(sc) {
        // return false;
        return this.getMinScale() > sc;
    }

    //获取最小缩放比
    getMinScale() {
        return 1;
        //console.log(cc.view.getCanvasSize().width,cc.view.getCanvasSize().height)
        //console.log(cc.winSize.width,cc.winSize.height)
        let windowSize =  cc.size(this.node.parent.getContentSize())//cc.winSize;
        let mapRect = this.node
        let scaleX = windowSize.width / mapRect.width;
        let scaleY = windowSize.height / mapRect.height;
        let scale = scaleX > scaleY ? scaleX : scaleY;
        //console.log("scale",scale)
        return scale;
    }
}
